/**
 * Simple regex experiment to create an automatic emoticons by Taufik Nurrohman
 * Visit: http://hompimpaalaihumgambreng.blogspot.com
 * Update: 23 August 2012
 */

$(function() {

	// Append an emoticon bar before comment-form
	if (putEmoAbove) {
		$(putEmoAbove).before('<div class="emoWrap"> :) :( =(  :s ^_^ :D   @@, ;) :-bd  :yaya: :&#39;(  :&#92;  B) :Q  7:(    &lt;3 0:)  :-a 7:O   x@ X@  &lt;:) &lt;=)    </div>');
	}
	function emo(emo, imgRep, emoKey) {
		$(emoRange).each(function() {
			$(this).html($(this).html().replace(/<br ?\/?>(:|;|=|\^)/ig, "<br> $1").replace(emo, " <img src='" + imgRep + "' class='emo delayLoad' alt='" + emoKey + "' />"));
		});
	}
	emo(/\s:\)+/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/smile.gif", ":)");
	emo(/\s;\)+/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/wink.gif", ";)");
	emo(/\s:\(/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/sad.gif", ":(");
	emo(/\s=\(/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/sadanimated.gif", "=(");
	emo(/\s@@,/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/rolleyes.gif", "@@,");
	emo(/\s:yaya:/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/yaya.gif", ":yaya:");
	emo(/\s:s/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/sullen.gif", ":s");
	emo(/\s:\\/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/memble.gif", ":&#92;");
	emo(/\s:D/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/haha.gif", ":D");
	emo(/\s\^(\_|)\^/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/senyum-tulus.gif", "^_^");
	emo(/\s:'\(/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/cry.gif", ":&#39;(");
	emo(/\sB\)/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/cool.gif", "B)");
	emo(/\s:Q/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/smoking.gif", ":Q");
	emo(/\s7:\(/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/conf.gif", "7:(");
	emo(/\s7:O/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/angry.gif", "7:O");
	emo(/\s&lt;3/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/love.gif", "&amp;amp;lt;3");
	emo(/\s0:\)+/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/angelgreen.gif", "0:)");
	emo(/\s:-a/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/vomit.gif", ":-a");
	emo(/\sx\@/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/marahbesar.gif", "x@");
	emo(/\s\X\@/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/arrgh.gif", "X@");
	emo(/\s:-bd/ig, "http://karina-project.googlecode.com/files/profilk.gif", ":-bd");
	emo(/\s&lt;:\)/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/festival1.gif", "&amp;amp;lt;:)");
	emo(/\s&lt;=\)/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/festival2.gif", "&amp;amp;lt;=)");
	var one = 0; // Show alert one times!

	// Click anywhere to hide the emoticon
	$(document.body).on("click", function() {
		$('.emoKey').remove();
	});

	// Click to show the code!
	$('.emo').css('cursor', 'pointer').on("click", function(e) {
		$('.emoKey').remove();
		$(this).after('<input class="emoKey" type="text" size="6" value=" ' + this.alt + '" />');
		$('.emoKey').trigger("select");
		if(emoMessage && one === 0) {
			alert(emoMessage);
			one = 1;
		}
		e.stopPropagation();
	});

});
